package test;

public class ArraySort {
	private  static Integer[] arr = {3,4,1,9,7,5,4,6};
	public static void main(String[] args) {
		bubbleSort(arr);
		selectionSort(arr);
		insertionSort(arr);
	}
	
	//冒泡
	public static Integer[] bubbleSort(Integer[] arr) {
		int temp;
		for (int i = 0; i < arr.length -1; i++) {
			for (int j = 0; j < arr.length -i -1; j++) {
				if (arr[j] < arr[j+1]) {
					temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
		
		for (Integer i : arr) {
			System.out.print(i + ",");
		}
		return arr;
	}
	
	//选择排序
	public static Integer[] selectionSort(Integer[] arr) {
		System.out.println("");
		int temp;
		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = i+1; j < arr.length; j++) {
				if (arr[i] < arr[j]) {
					temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}
		for (Integer i : arr) {
			System.out.print(i + ",");
		}
		return arr;
	}
	
	//插入排序
	public static Integer[] insertionSort(Integer[] arr) {
		System.out.println("");
		for (int i = 1; i < arr.length; i++) {
			int temp = arr[i];
			int j;
			for (j = i -1; j >= 0 && temp < arr[j]; j--) {
				arr[j+1] = arr[j];
			}
			arr[j+1] = temp;
		}
		for (Integer i : arr) {
			System.out.print(i + ",");
		}
		return arr;
	}
}
